package com.ext.portlet.common;

import java.util.List;

import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.service.RoleServiceUtil;

public class CommonUtil {
	
	public static boolean isAdmin(User user) {
        return isUserInRole(user, "Administrator");
	}
	
	public static boolean isPowerUser(User user) {
        return isUserInRole(user, "Power User");
	}
	
	public static boolean isUser(User user) {
        return isUserInRole(user, "User");
	}
	
	public static boolean isUserInRole(User user, String role) {
        boolean flag = false;

        if (user == null) return false;

        try {
                List<Role> roles = RoleServiceUtil.getUserRoles(user.getUserId());
                for (int i = 0; i < roles.size(); i++) {
                        Role role1 = (Role) roles.get(i);
                        if (role1.getName().equalsIgnoreCase(role)) {
                                flag = true;
                                break;
                        }
                }
        } catch (Exception e) {
              //  log.error(e.getMessage());
        }
        return flag;
}


}
